package com.dynamixsoftware.printershare;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.net.Uri;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Instances;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.widget.Toast;
import com.dynamixsoftware.printershare.ActivityPrintText;
import com.dynamixsoftware.printershare.App;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.Map.Entry;

public class ActivityPrintCalendar extends ActivityPrintText {

	private static final int TYPE_DAY = 0;
	private static final int TYPE_MEETING = 1;

	private ArrayList calculateDays(Cursor var1, int var2, int var3) {
		TreeMap var4 = new TreeMap();

		for (int var5 = 0; var1.moveToNext(); ++var5) {
			int var12 = var1.getInt(9);
			int var13 = var1.getInt(10);

			for (int var14 = Math.max(var12, var2); var14 <= var13 && var14 <= var3; ++var14) {
				LinkedList var15 = (LinkedList) var4.get(Integer.valueOf(var14));
				if (var15 == null) {
					var15 = new LinkedList();
					var4.put(Integer.valueOf(var14), var15);
				}

				var15.add(Integer.valueOf(var5));
			}
		}

		ArrayList var6 = new ArrayList();
		Iterator var7 = var4.entrySet().iterator();

		while (var7.hasNext()) {
			Entry var8 = (Entry) var7.next();
			var6.add(new ActivityPrintCalendar.RowInfo(0, ((Integer) var8.getKey()).intValue()));
			Iterator var10 = ((LinkedList) var8.getValue()).iterator();

			while (var10.hasNext()) {
				var6.add(new ActivityPrintCalendar.RowInfo(1, ((Integer) var10.next()).intValue()));
			}
		}

		return var6;
	}

	protected void createPages() {
		this.initPage();
		Bundle var1 = this.getIntent().getExtras();
		long var2 = var1.getLong("start");
		long var4 = var1.getLong("end");
		int var6 = TimeZone.getDefault().getOffset(var2) / 1000;
		int var7 = TimeZone.getDefault().getOffset(var4) / 1000;
		int var8 = Time.getJulianDay(var2, (long) var6);
		int var9 = Time.getJulianDay(var4, (long) var7);
		Cursor var10 = ActivityPrintCalendar.Impl.getImpl().createCursor(this, var2, var4);
		ArrayList var11;
		if (var10 != null) {
			var11 = this.calculateDays(var10, var8, var9);
		} else {
			var11 = new ArrayList();
		}

		int var12 = -1;

		while (true) {
			int var13 = var11.size();
			if (var12 >= var13) {
				this.addPage();
				if (var10 != null) {
					var10.close();
				}

				if (var11.size() == 0) {
					this.runOnUiThread(new Runnable() {
						public void run() {
							Toast.makeText(ActivityPrintCalendar.this, 2131099686, 1).show();
						}
					});
				}

				return;
			}

			this.newPage();
			if (var12 != -1) {
				ActivityPrintCalendar.RowInfo var14 = (ActivityPrintCalendar.RowInfo) var11.get(var12);
				if (var14.mType == 0) {
					Time var15 = new Time();
					var15.set((new Date()).getTime());
					long var16 = var15.setJulianDay(var14.mData);
					String var18;
					if (var14.mData == Time.getJulianDay(System.currentTimeMillis(), var15.gmtoff)) {
						var18 = "Today, " + DateUtils.formatDateTime(this, var16, 20);
					} else {
						var18 = DateUtils.formatDateTime(this, var16, 20 | 2);
					}

					Paint var19 = App.newPaint();
					var19.setStyle(Style.FILL);
					var19.setColor(-16777216);
					int var20 = this.testTextSize(var18, 70, true, 0.0F, 20);
					if (this.needNew(10, var20 + 65)) {
						--var12;
					} else {
						this.printRect(-2565928, 0, 0, this.page.getWidth() - 2 * this.m,
								(int) ((float) (var20 + 20) * this.fontSizeCoef));
						this.printText(var18, 70, true, 0.0F, 20, 70, 90, var19);
					}
				} else {
					var10.moveToPosition(var14.mData);
					String var22 = var10.getString(0);
					String var23 = var10.getString(12);
					long var24 = var10.getLong(6);
					long var26 = var10.getLong(7);
					boolean var28;
					if (var10.getInt(2) != 0) {
						var28 = true;
					} else {
						var28 = false;
					}

					int var29;
					if (var28) {
						var29 = 8192;
					} else {
						var29 = 1;
					}

					if (DateFormat.is24HourFormat(this)) {
						var29 |= 128;
					}

					String var30 = DateUtils.formatDateRange(this, var24, var26, var29);
					String var31 = var10.getString(1);
					int var32 = ActivityPrintCalendar.Impl.getImpl().getColor(this, var10);
					Paint var33 = App.newPaint();
					var33.setStyle(Style.FILL);
					var33.setColor(var32);
					int var34 = this.th;
					int var10000 = (int) (-5.0F * this.fontSizeCoef);
					String var36;
					if (var22 != null) {
						var36 = var22;
					} else {
						var36 = "";
					}

					int var37 = this.testTextSize(var36, 45, true, 0.0F, 30);
					if ((float) this.th + (float) var37 * this.fontSizeCoef > (float) (-10 + (this.page.getHeight() - 2 * this.m))) {
						if (!this.needNew(0, 45)) {
							int var52 = (int) (-5.0F * this.fontSizeCoef);
							if (var23 != null && var23.length() > 0 || var30 != null && var30.length() > 0
									|| var31 != null && var31.length() > 0) {
								boolean var53 = false;
							}

							this.printRect(-16777216, 0, 0, (int) (8.0F * this.fontSizeCoef), this.page.getHeight() - 2
									* this.m - this.th);
						}

						var34 = 0;
					}

					String var38;
					if (var22 != null) {
						var38 = var22;
					} else {
						var38 = "";
					}

					this.printText(var38, 45, true, 0.0F, 30, 45, 55, var33);
					int var39 = (int) (-5.0F * this.fontSizeCoef);
					if (var23 != null && var23.length() > 0 || var30 != null && var30.length() > 0 || var31 != null
							&& var31.length() > 0) {
						var39 = 0;
					}

					this.printRect(-16777216, 0, (int) ((float) (-(this.th - var34)) + 10.0F * this.fontSizeCoef),
							(int) (8.0F * this.fontSizeCoef), var39);
					int var40 = this.th;
					var33.setColor(-16777216);
					if (var23 != null && var23.length() > 0) {
						int var49 = this.testTextSize(var23, 25, true, 0.0F, 30);
						if ((float) this.th + (float) var49 * this.fontSizeCoef > (float) (-10 + (this.page.getHeight() - 2 * this.m))) {
							if (!this.needNew(0, 25)) {
								var10000 = (int) (-5.0F * this.fontSizeCoef);
								if ((var30 == null || var30.length() <= 0) && var31 != null && var31.length() > 0) {
									;
								}

								this.printRect(-16777216, 0, 0, (int) (8.0F * this.fontSizeCoef), this.page.getHeight()
										- 2 * this.m - this.th);
							}

							var40 = 0;
						}

						this.printText(var23, 25, true, 0.0F, 30, 25, 35, var33);
						int var50 = (int) (-5.0F * this.fontSizeCoef);
						if (var30 != null && var30.length() > 0 || var31 != null && var31.length() > 0) {
							var50 = 0;
						}

						this.printRect(-16777216, 0, -1 + -(this.th - var40), (int) (8.0F * this.fontSizeCoef), var50);
					}

					int var41 = this.th;
					if (var30 != null && var30.length() > 0) {
						int var46 = this.testTextSize(var30, 35, true, 0.0F, 30);
						if ((float) this.th + (float) var46 * this.fontSizeCoef > (float) (-10 + (this.page.getHeight() - 2 * this.m))) {
							if (!this.needNew(0, 35)) {
								var10000 = (int) (-5.0F * this.fontSizeCoef);
								if ((var30 == null || var30.length() <= 0) && var31 != null && var31.length() > 0) {
									;
								}

								this.printRect(-16777216, 0, 0, (int) (8.0F * this.fontSizeCoef), this.page.getHeight()
										- 2 * this.m - this.th);
							}

							var41 = 0;
						}

						this.printText(var30, 35, true, 0.0F, 30, 35, 45, var33);
						int var47 = (int) (-5.0F * this.fontSizeCoef);
						if (var31 != null && var31.length() > 0) {
							var47 = 0;
						}

						this.printRect(-16777216, 0, -1 + -(this.th - var41), (int) (8.0F * this.fontSizeCoef), var47);
					}

					int var42 = this.th;
					if (var31 != null && var31.length() > 0) {
						int var44 = this.testTextSize(var31, 30, false, 0.0F, 30);
						if ((float) this.th + (float) var44 * this.fontSizeCoef > (float) (-10 + (this.page.getHeight() - 2 * this.m))) {
							if (!this.needNew(0, 30)) {
								var10000 = (int) (-5.0F * this.fontSizeCoef);
								if ((var30 == null || var30.length() <= 0) && var31 != null && var31.length() > 0) {
									;
								}

								this.printRect(-16777216, 0, 0, (int) (8.0F * this.fontSizeCoef), this.page.getHeight()
										- 2 * this.m - this.th);
							}

							var42 = 0;
						}

						this.printText(var31, 30, false, 0.0F, 30, 30, 40, var33);
						this.printRect(-16777216, 0, -1 + -(this.th - var42), (int) (8.0F * this.fontSizeCoef),
								(int) (-5.0F * this.fontSizeCoef));
					}

					Paint var43 = App.newPaint();
					var43.setStyle(Style.FILL);
					var43.setColor(var32);
				}
			}

			++var12;
		}
	}

	private static class ImplOld extends ActivityPrintCalendar.Impl {

		private ImplOld() {
		}

		// $FF: synthetic method
		ImplOld(Object var1) {
			this();
		}

		Cursor createCursor(Context var1, long var2, long var4) {
			Cursor var6 = null;
			Uri[] var7 = new Uri[] { Uri.parse("content://com.android.calendar/instances/when/" + var2 + "/" + var4),
					Uri.parse("content://calendar/instances/when/" + var2 + "/" + var4) };

			for (int var8 = 0; var8 < var7.length; ++var8) {
				var6 = var1.getContentResolver().query(
						var7[var8],
						new String[] { "title", "eventLocation", "allDay", "hasAlarm", "color", "rrule", "begin",
								"end", "event_id", "startDay", "endDay", "selfAttendeeStatus", "description" },
						"selected=1 AND selfAttendeeStatus!=2", (String[]) null, "startDay ASC, begin ASC, title ASC");
				if (var6 != null) {
					break;
				}
			}

			return var6;
		}

		int getColor(Context var1, Cursor var2) {
			return -16777216 | var2.getInt(4);
		}
	}

	@SuppressLint("NewApi")
	private static class ImplNew extends ActivityPrintCalendar.Impl {

		private ImplNew() {
		}

		// $FF: synthetic method
		ImplNew(Object var1) {
			this();
		}

		Cursor createCursor(Context var1, long var2, long var4) {
			int var6 = TimeZone.getDefault().getOffset(var2) / 1000;
			int var7 = TimeZone.getDefault().getOffset(var4) / 1000;
			int var8 = Time.getJulianDay(var2, (long) var6);
			int var9 = Time.getJulianDay(var4, (long) var7);
			return var1.getContentResolver().query(
					Uri.withAppendedPath(Instances.CONTENT_BY_DAY_URI, "//" + var8 + "//" + var9),
					new String[] { "title", "eventLocation", "allDay", "hasAlarm", "eventColor", "rrule", "begin",
							"end", "event_id", "startDay", "endDay", "selfAttendeeStatus", "description", "eventColor",
							"calendar_id" }, "selfAttendeeStatus!=2", (String[]) null,
					"startDay ASC, begin ASC, title ASC");
		}

		int getColor(Context var1, Cursor var2) {
			if (!var2.isNull(13)) {
				return -16777216 | var2.getInt(13);
			} else {
				Cursor var3 = var1.getContentResolver().query(Calendars.CONTENT_URI, new String[] { "calendar_color" },
						"_id= " + var2.getInt(14), (String[]) null, (String) null);
				return var3 != null && var3.moveToFirst() ? -16777216 | var3.getInt(0) : -16777216;
			}
		}
	}

	private static class RowInfo {

		final int mData;
		final int mType;

		RowInfo(int var1, int var2) {
			this.mType = var1;
			this.mData = var2;
		}
	}

	abstract static class Impl {

		static ActivityPrintCalendar.Impl getImpl() {
			return (ActivityPrintCalendar.Impl) (Integer.parseInt(VERSION.SDK) < 14 ? new ActivityPrintCalendar.ImplOld(
					null) : new ActivityPrintCalendar.ImplNew(null));
		}

		abstract Cursor createCursor(Context var1, long var2, long var4);

		abstract int getColor(Context var1, Cursor var2);
	}
}
